package com.mytaobao.main.weitao;

import android.content.Context;
import android.graphics.Canvas;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;

import com.baseproject.util.LogUtils;
import com.facebook.stetho.common.LogUtil;

/**
 * Created by hp on 2017/4/14.
 */

public class WeiTaoTabLayout extends TabLayout {

    public WeiTaoTabLayout(Context context) {
        super(context);
    }

    public WeiTaoTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WeiTaoTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int appBarOffsetWhenShrink = 0;
    WeiTaoAppBarLayoutBehavior mWeiTaoAppBarLayoutBehavior;

    public void setAppBarOffsetWhenShrink(int height, WeiTaoAppBarLayoutBehavior weiTaoAppBarLayoutBehavior) {
        this.appBarOffsetWhenShrink = height;
        this.mWeiTaoAppBarLayoutBehavior = weiTaoAppBarLayoutBehavior;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        //获取高度的模式和尺寸
//        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//        if (tabHeight != 0 ) {
//            heightSize = tabHeight;
//            heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
//        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        LogUtils.d("[onLayout] before super this = " + this);
        super.onLayout(changed, l, t, r, b);
        LogUtil.d("[onLayout] after super this = " + this);
        adjuestAppbarPosition();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        LogUtil.d("[onDraw] before super this = " + this);
        super.onDraw(canvas);
        LogUtil.d("[onDraw] after super this = " + this);

    }

    public void adjuestAppbarPosition() {




        if(appBarOffsetWhenShrink != 0 && mWeiTaoAppBarLayoutBehavior != null
                && mWeiTaoAppBarLayoutBehavior.getTabStatus() == WeiTaoAppBarLayoutBehavior.TabStatus.TAB_STATUS_SHRINK) {
            mWeiTaoAppBarLayoutBehavior.setTopAndBottomOffset(appBarOffsetWhenShrink);
//            AppBarLayout appBarLayout = (AppBarLayout) this.getParent();
//            appBarLayout.offsetTopAndBottom(-appBarOffsetWhenShrink);//由于viewPager通过Behavior依赖的view是AppBarLayout，因此移动AppBarLayout才能引起viewpager的联动
        }
    }
}
